home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / engclock_v7.0.lha / EngClock_v7.0 / EngClock7_Source / auto.c < prev    next >
C/C++ Source or Header  |  1996-01-04  |  6KB  |  227 lines

  1. #include <clib/intuition_protos.h>
  2. #include <clib/exec_protos.h>
  3. #include <clib/dos_protos.h>
  4. #include <intuition/intuition.h>
  5. #include <exec/exec.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10.  
  11. extern struct Window *win_p;
  12. extern struct NewWindow win1;
  13. extern struct Menu menu1;
  14. extern char line1[200], line2[200];
  15. extern void msg(char *text);
  16. extern void closeall(void);
  17.  
  18. extern struct {
  19. /* Font structure, set up by getfont() and used globally */
  20.  
  21.    int x;            /* Width of default graphic font */
  22.    int y;            /* Height of default graphic font */
  23. }font;
  24.  
  25. struct Event {
  26.     BOOL used;
  27.     int type;    /* Type of event */
  28.     int hours;    /* Time of event */
  29.     int minutes;
  30.     int timecode;
  31.     char message[256];
  32.     int day;
  33.     int month;
  34.     int year;
  35.     BOOL enabledate;
  36.     int freq;
  37. };
  38.  
  39. extern struct {
  40. /* Prefs structure used for preferences file format */
  41.  
  42.    char header[10];  /* I.D. Header */
  43.    int vers;         /* Version of preferences file */
  44.    int x;            /* X dimension of window when saved */
  45.    int y;            /* Y dimension of window when saved */
  46.    int width;        /* Width of window when saved */
  47.    int height;       /* Height of window when saved */
  48.    int planguage;    /* Language selected (menu) when saved, low pri */
  49.    int just;         /* Justification of text, 0=Left, 1=Centre */
  50.    short date;       /* Display date?, 0 = No, 1=Yes */
  51.    short wtf;         /* Window to front? */
  52.    int time_col[8];
  53.    int date_col[8];
  54.    short autoadjust;       /* Auto adjust ? */
  55.    char backdrop[256];
  56.    char pub[139];
  57.     char accent[256];        /* Language file for tRanslate */
  58.     char tkey[100];
  59.     char hkey[100];
  60.     struct Event events[11];  /* For the alarm! */ 
  61. }prefs;
  62. extern struct {
  63.    int s_width;       /* Width of current screen */
  64.    int s_height;      /* Height of current screen */
  65.    int s_bar;         /* Bar height of windows.  Other font */
  66.    int s_cols;        /* Number of colours available */
  67.    int s_mode;        /* Mode according to s_cols */
  68. }screentype;
  69.  
  70. void autoadjust(void) {
  71.     WORD x,y,width,height;
  72.     int length1, length2;
  73.     WORD newwidth;
  74.  
  75.     // Get current dimensions
  76.  
  77.     // Before going any further we should make sure that the Window's
  78.     //    MinWidth and MinHeight are set LOW LOW LOW!
  79.  
  80.     if(win_p->MinWidth != 40 || win_p->MinHeight != screentype.s_bar+font.y+5) {
  81.         // Update win structure
  82.         win1.LeftEdge=win_p->LeftEdge;
  83.         win1.TopEdge=win_p->TopEdge;
  84.         win1.Height=win_p->Height;
  85.         win1.Width=win_p->Width;
  86.  
  87.         win1.MinWidth=40;
  88.         win1.MinHeight=screentype.s_bar+font.y+5;
  89.  
  90.         ClearMenuStrip(win_p);
  91.         CloseWindow(win_p);
  92.  
  93.         if(!(win_p=OpenWindow(&win1))) {
  94.             // There is no reason why this should fail since we updated
  95.             msg("Error re-opening window (system failure!!)");
  96.             closeall();    
  97.             exit(NULL);
  98.         }
  99.         SetMenuStrip(win_p,&menu1);
  100.     }
  101.     Forbid();
  102.     x=win_p->LeftEdge; y=win_p->TopEdge;
  103.     width=win_p->Width; height=win_p->Height;
  104.     Permit();
  105.  
  106.     // Get string lengths
  107.  
  108.     length1=strlen(line1);
  109.     length2=strlen(line2);
  110.  
  111.  
  112.     // Which one is bigger?
  113.  
  114.     if(length2 > length1)
  115.         length1=length2;
  116.  
  117.     // Now the greater length is in length1
  118.  
  119.     // What is the pixel requirements??
  120.  
  121.     newwidth=length1*font.x;
  122.     newwidth+=16; /* Initial offset from LHS */
  123.     newwidth+=16; /* Keep it even each side */
  124.     newwidth+=30; /* RHS border */
  125.  
  126.     // Do we need to alter the window size?
  127.  
  128.     if(newwidth==width) {
  129.         return;
  130.  
  131.     }
  132.     // Ok then, what sort of stretch/shrink are we looking at?
  133.     
  134.     /* prefs.just == 0 is a stretch to the right,
  135.         2 is a strech to the left
  136.         1 is a proportional stretch */
  137.  
  138.     switch(prefs.just) {
  139.         case 0: /* Left justification */
  140.             // Stretch window to the right
  141.  
  142.             // Are we doing a stretch or a shrink
  143.  
  144.             if(newwidth < width) {
  145.                 // Shrink
  146.                 Forbid();
  147.                 ChangeWindowBox(win_p,x,y,newwidth,height);
  148.                 Permit();
  149.             }
  150.             else {
  151.                 // Stretch
  152.                 // Do we have enough room?
  153.  
  154.                 while(x+newwidth > screentype.s_width) {
  155.                     // Try to solve the problem by reducing x
  156.                     if(x==0) {
  157.                         // Just too small a screen!
  158.                         msg("Sorry cannot autoadjust, screen too small!");
  159.                         return;
  160.                     }
  161.                     x--;
  162.                 }
  163.                 Forbid();
  164.                 ChangeWindowBox(win_p,x,y,newwidth,height);
  165.                 Permit();
  166.             }            
  167.         break;
  168.         case 1: /* Centre justification */
  169.             // Stretch window equally in both directions
  170.  
  171.             // Are we doing a stretch or a shrink
  172.  
  173.             if(newwidth < width) {
  174.                 // Shrink
  175.                 Forbid();
  176.                 ChangeWindowBox(win_p,x+((width-newwidth)/2),y,newwidth,height);
  177.                 Permit();
  178.             }
  179.             else {
  180.                 // Stretch
  181.                 // Do we have enough room?
  182.  
  183.                 while(x-((newwidth-width)/2)+newwidth > screentype.s_width) {
  184.                     // Try to solve the problem by reducing x
  185.                     if(x==0) {
  186.                         // Just too small a screen!
  187.                         msg("Sorry cannot autoadjust, screen too small!");
  188.                         return;
  189.                     }
  190.                     x--;
  191.                 }
  192.                 Forbid();
  193.                 ChangeWindowBox(win_p,x-((newwidth-width)/2),y,newwidth,height);
  194.                 Permit();
  195.             }            
  196.         break;
  197.         case 2: /* Right */
  198.             // Stretch window to the left
  199.  
  200.             // Are we doing a stretch or a shrink
  201.  
  202.             if(newwidth < width) 
  203.                 // Shrink
  204.                 ChangeWindowBox(win_p,x+(width-newwidth),y,newwidth,height);
  205.             else {
  206.                 // Stretch
  207.                 // Do we have enough room?
  208.  
  209.                 while(x-(newwidth-width)+newwidth > screentype.s_width) {
  210.                     // Try to solve the problem by reducing x
  211.                     if(x==0) {
  212.                         // Just too small a screen!
  213.                         msg("Sorry cannot autoadjust, screen too small!");
  214.                         return;
  215.                     }
  216.                     x--;
  217.                 }
  218.                 ChangeWindowBox(win_p,x-(newwidth-width),y,newwidth,height);
  219.             }            
  220.         break;
  221.         default:
  222.             return;
  223.         break;
  224.     }
  225.     Delay(5);
  226. }    
  227.